#ifndef XWinSystem_First
#define XWinSystem_First
#ifdef __GNUG__
#pragma interface
#endif

#include "X11.h"
#include "WindowSystem.h"
#include "Symbol.h"

class XWindowPort;
class XIAC;
class XInputHandler;
class XDragDropService;

const int cMaxCursors= 30;

#define MAX_PROP_WORDS 100000

//---- EtAtom ------------------------------------------------------------------

enum XAtomMode { eAtomDontCreate, eAtomCreate };

class EtAtom {
	Atom InternAtom();
public:
	EtAtom(char *nm, XAtomMode m= eAtomCreate);
	bool Exists()
		{ return InternAtom() != 0; }
	Atom XA()
		{ return InternAtom(); }
private:
	char *name;
	XAtomMode mode;
	bool valid;
	Atom atom;
};

extern EtAtom
	cIsEtWindow,
	cTakeFocus,
	cSaveYourself,
	cDeleteWindow,
	cTargets,
	cMultiple,
	cTimestamp,
	cXViewDragLoad,
	cOpenLookDecorAdd,
	cOpenLookDecorDel,
	cOpenLookDecorFooter,
	cOpenLookWindowBusy,
	cOpenLookResize,
	cOpenLookWinAttr,
	cOpenLookWinCmd,
	cOpenLookWinBase,
	cDecWmMoved;

//---- XWinSystem --------------------------------------------------------------

class XWinSystem: public WindowSystem {
public:
	long bellduration;
	XWindow awin, xholder;
	XIAC *rpc;
	XInputHandler *inputhandler;
	XDragDropService *dragDropService;

public:
	static XCursor CurMap[cMaxCursors];
	static XDisplay *display;
	static XWindow rootwin, busy_winid;
	static int screen;
	static u_long black, white;
	static Visual *dfltvisual;
	static Pixmap batchPixmap;
	static bool allow;
		
public:
	MetaDef(XWinSystem);
	XWinSystem();
	~XWinSystem();

	bool Init0();

	void DevCheckSelection();
	void SendClipBoard(XSelectionRequestEvent *sre);
	int DevHaveSelection(Data *data);

	bool Notify();
	bool HasInterest();
	WindowPort *MakeWindow(Window *bw, WindowType wt, GrCursor curs);
	VObject *MakeWindowBorder(WindowType wt, VObject *in,
											char *title, bool block, bool fixed);
	FontManager *MakeFontManager();
	DevBitmap *MakeDevBitmap(Point sz, u_short *data, u_short depth, bool freedata,
																u_short pad);
	IACImpl *MakeRpcImpl(IAC *sc);
	void graphicDelay(unsigned int);
	void DevBell(long duration);
	long GetCurrentServerTime();
	int DevFindWindow(const Point &p);
	bool DevIsEtWindow(int winid);
	int DevDragDrop(int code, int winid, const Point &globalpos, Rectangle *droprect);
	void HandleError(XErrorEvent *ev);
	char *GetProperty(XWindow win, const Atom &atom, bool del= FALSE, u_long *len= 0);

	static Pixmap CreatePixmap(Point e, int d)
		{ return XCreatePixmap(display, rootwin, e.x, e.y, d); }
	static GC CreateGC(Drawable d, u_long valuemask, XGCValues *values)
		{ return XCreateGC(display, d, valuemask, values); }
	static void Allow();
	static void SetBusy(XWindow winid);
	static XCursor UseCursor(GrCursor c);

	static void Use();
};

extern WindowSystem *NewXWindowSystem();

#endif
